package com.lc.pianostudent.conn;

import com.lc.pianostudent.bean.HomeNewsBean;
import com.lc.pianostudent.bean.PianoIndexBean;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/2/22.
 */
@HttpInlet(Conn.GETBANNER)
public class PostGetBanner extends BaseAsyPost {

    public String page,type;
    public PostGetBanner(AsyCallBack asyCallBack) {
        super(asyCallBack);
    }

    @Override
    protected PostGetBannerInfo parser(JSONObject object) throws Exception {
        if (object.optString("code").equals("200")) {

            PostGetBannerInfo info = new PostGetBannerInfo();

            info.code = object.optString("code");

            JSONArray array = object.optJSONArray("banner");
            if(array != null || array.length() != 0){
                for(int i = 0;i<array.length();i++){
                    JSONObject o = array.optJSONObject(i);
                    info.bannerList.add(o.optString("picurl"));
                    info.linkUrl.add(o.optString("linkurl"));
                }

            }

            JSONObject jsonObject = object.optJSONObject("list");
            info.total = jsonObject.optInt("total");
            info.per_page = jsonObject.optInt("per_page");
            info.current_page = jsonObject.optInt("current_page");
            info.last_page = jsonObject.optInt("last_page");

            JSONArray arraylist = jsonObject.optJSONArray("data");

            if (arraylist != null  || arraylist.length() != 0) {

                for (int i = 0; i < arraylist.length(); i++) {

                    JSONObject jo = arraylist.optJSONObject(i);

                    HomeNewsBean homeNewsBean = new HomeNewsBean();
                    homeNewsBean.setTitle(jo.optString("title"));
                    homeNewsBean.setClassid(jo.optString("classid"));
                    homeNewsBean.setIntro(jo.optString("intro"));
                    homeNewsBean.setLinkurl(jo.optString("linkurl"));
                    homeNewsBean.setPicurl(jo.optString("picurl"));
                    homeNewsBean.setId(jo.optString("id"));
                    homeNewsBean.setUrl(jo.optString("url"));
                    homeNewsBean.setCreate_time(jo.optString("create_time"));
                    homeNewsBean.setColor(jo.optString("color"));
                    homeNewsBean.setCrude(jo.optString("crude"));
                    homeNewsBean.setTop(jo.optString("top"));


                    info.homeNewsList.add(homeNewsBean);
                }

            }


            TOAST = object.optString("message");
            return info;

        } else {

            TOAST = object.optString("message");

        }

        return null;
    }

    public static class PostGetBannerInfo {

        public String code;
        public List<String> bannerList= new ArrayList<>();
        public List<HomeNewsBean> homeNewsList= new ArrayList<>();
        public List<String> linkUrl = new ArrayList<>();
        public int total, per_page ,current_page,last_page;
    }

}
